package com.tongle.frs.common.util;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.net.URLEncoder;
import java.net.UnknownHostException;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Random;

public class Globals
{
  public static String getCtx(HttpServletRequest request)
  {
    String ctx = "";
    int port = request.getServerPort();
    String strport = "";
    if (port != 80) {
      strport = ":" + port;
    }
    ctx = request.getScheme() + "://" + request.getServerName() + strport + request.getContextPath();
    return ctx;
  }

  public static boolean isAjaxRequest(HttpServletRequest request)
  {
    return request.getHeader("x-requested-with") != null;
  }

  public static String getCookiePath(HttpServletRequest request)
  {
    String path = request.getContextPath();
    if (StringUtils.isBlank(path)) {
      path = "/";
    }
    return path;
  }

  public static String getRealIP(HttpServletRequest request)
  {
    String ip = "";
    if (request != null) {
      ip = request.getHeader("cdn-src-ip");
      if ((ip == null) || (ip.length() == 0) || ("unknown".equalsIgnoreCase(ip))) {
        ip = request.getHeader("x-real-ip");
      }
      if ((ip == null) || (ip.length() == 0) || ("unknown".equalsIgnoreCase(ip))) {
        ip = request.getHeader("x-forwarded-for");
      }
      if ((ip == null) || (ip.length() == 0) || ("unknown".equalsIgnoreCase(ip))) {
        ip = request.getHeader("proxy-client-ip");
      }
      if ((ip == null) || (ip.length() == 0) || ("unknown".equalsIgnoreCase(ip))) {
        ip = request.getHeader("wl-proxy-client-ip");
      }
      if ((ip == null) || (ip.length() == 0) || ("unknown".equalsIgnoreCase(ip))) {
        ip = request.getRemoteAddr();
      }
      if (ip != null) {
        ip = ip.replaceAll(";", ",");
        String[] ips = ip.split(",");
        if (ips.length < 1)
          ip = "";
        else if (ips.length == 1)
          ip = ips[0];
        else if (!ips[0].equalsIgnoreCase("127.0.0.1"))
          ip = ips[0];
        else {
          ip = ips[1];
        }
      }
    }
    if ((ip != null) && (ip.length() > 0)) {
      ip = ip.trim();
    }
    return ip;
  }

  public static String getLocalIp()
    throws UnknownHostException
  {
    InetAddress localHostAddress = InetAddress.getLocalHost();
    return localHostAddress.getHostAddress();
  }

  public static String getRandomCode(int n)
  {
    String rcode = "";
    Random random = new Random();
    for (int i = 0; i < n; i++) {
      rcode = rcode + random.nextInt(10) + "";
    }
    return rcode;
  }

  public static String getRequestParams(HttpServletRequest request)
  {
    StringBuffer params = new StringBuffer();
    if (request != null) {
      Enumeration names = request.getParameterNames();
      if (names != null) {
        while (names.hasMoreElements()) {
          String name = (String)names.nextElement();
          try {
            params.append("&" + name + "=" + URLEncoder.encode(request.getParameter(name), "utf-8"));
          }
          catch (Exception localException)
          {
          }
        }
      }
    }
    return params.length() > 0 ? params.substring(1) : "";
  }

  public static JSONObject getRequestParamsJson(HttpServletRequest request)
  {
    Map paramsMap = request.getParameterMap();

    JSONObject jsonObject = new JSONObject();

    Iterator iter = paramsMap.entrySet().iterator();
    while (iter.hasNext()) {
      Entry entry = (Entry)iter.next();

      String key = (String)entry.getKey();
      String value = ((String[])entry.getValue())[0];

      jsonObject.put(key, value);
    }
    return jsonObject;
  }
}